package com.nsgf.library.ui.page;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.nsgf.library.BR;
import com.nsgf.library.R;
import com.nsgf.library.data.api.APIs;
import com.nsgf.library.data.bean.UserInfo;
import com.nsgf.library.data.bean.WorkOrderDetailBean;
import com.nsgf.library.data.bean.XBannerBeanInfo;
import com.nsgf.library.databinding.ActivityProjectOverviewDetailsBinding;
import com.nsgf.library.domain.request.AccountRequester;
import com.nsgf.library.glide.GlideImgManager;
import com.nsgf.library.photopicker.PhotoPreview;
import com.nsgf.library.photopicker.widget.MultiPickResultView;
import com.stx.xhb.xbanner.XBanner;

import java.util.ArrayList;
import java.util.List;


/**
 * 工程概况
 */
public class ProjectOverviewActivity extends BaseActivity {

    private ProjectOverviewInfoStates mStates;


    private List<Fragment> fragmentList = new ArrayList<>();

    private String[] mPageTitleList;
    private AccountRequester mAccountRequester;

    private ActivityProjectOverviewDetailsBinding mBinding;

    @Override
    protected void initViewModel() {
        mStates = new ProjectOverviewInfoStates();
        mAccountRequester = getActivityScopeViewModel(AccountRequester.class);
    }

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        return new DataBindingConfig(R.layout.activity_project_overview_details, BR.vm, mStates);
    }

    private List<XBannerBeanInfo> banners = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getLifecycle().addObserver(mAccountRequester);
        mStates.titleBar.set(getIntent().getStringExtra("title"));
        mBinding = (ActivityProjectOverviewDetailsBinding) getBinding();

        // 获取水库信息
        mAccountRequester.reservoirInfo(UserInfo.getInstance().getProjectCode(), UserInfo.getInstance().getFacilityType());

        mAccountRequester.getReservoirResult().observe(this, dataResult -> {

            if (dataResult.getResponseCode() == 200) {

                mStates.projectBeanState.set(dataResult.getData());

                banners.clear();
                if (null != dataResult.getData().getFiles()) {
                    for (WorkOrderDetailBean.FileDto fileDto : dataResult.getData().getFiles()) {
                        banners.add(new XBannerBeanInfo(APIs.BASE_URL_IMG + fileDto.getFilePath()));
                    }
                    mBinding.xbanner.setBannerData(banners);
                    mBinding.xbanner.loadImage((banner, model, view, position) -> {
                        String url = (String) ((XBannerBeanInfo) model).getXBannerUrl();
                        GlideImgManager.getInstance().glideLoaderBannerCenterCrop(ProjectOverviewActivity.this, url.replace("\\", "/"), (ImageView) view, 10);

                    });
                    mBinding.xbanner.setOnItemClickListener(new XBanner.OnItemClickListener() {
                        @Override
                        public void onItemClick(XBanner banner, Object model, View view, int position) {
                            ArrayList<WorkOrderDetailBean.FileDto> fileDtos = new ArrayList<>();
                            WorkOrderDetailBean.FileDto fileDto = new WorkOrderDetailBean.FileDto();
                            fileDto.setFilePath(banners.get(position).getXBannerUrl().toString());
                            fileDtos.add(fileDto);
                            PhotoPreview.builder().setPhotos(fileDtos).setAction(MultiPickResultView.ACTION_ONLY_SHOW).start(ProjectOverviewActivity.this);
                        }
                    });
                }
            } else {

                if (401 == dataResult.getResponseCode()) {
                    UserInfo.getInstance().clearUserInfo();
                    finish();
                }
            }
        });
    }

    public static class ProjectOverviewInfoStates extends StateHolder {

        /**
         * 判断是否是 water.obj.type.3 类型
         */
        public boolean isWaterObjType3() {
            return "water.obj.type.3".equals(UserInfo.getInstance().getFacilityType());
        }
    }
}
